/**
 *
 * Copyright 2012 TeleNav, Inc. All rights reserved.
 * DataUtil.java
 *
 */
package com.telenav.snowsweeper.util;

import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.telenav.snowsweeper.data.Edge;
import com.telenav.snowsweeper.data.EdgeMap;
import com.telenav.snowsweeper.data.PassPoint;
import com.telenav.snowsweeper.data.Point;
import com.telenav.snowsweeper.data.PointsManager;

/**
 * @author hhzhu
 * @date 2012-12-7
 */
public class DataUtil
{
    public static final String SERVER_RESPONSE_OK = "OK";
    public static final String SERVER_RESPONSE_INVALID_ROUTE = "INVALID_ROUTE";
    public static final String SERVER_RESPONSE_TIME_OUT = "TIME_OUT";
    public static final String fakePassPoint = "{\"status\": \"OK\",\"count\": 5,\"latitude\": 2.0,\"longitude\": 1.0,\"altitude\":0.0,\"speed\": 0.0,\"heading\": 0,\"edgeId\": \"\",\"timestamp\": 4000}";
    public static final String fakeHint = "{\"status\": \"OK\",\"routes\": [ {\"route\": [{ \"id\": \"01\" },{ \"id\": \"02\" },{ \"id\": \"12\" }] } ] }";
    public static final String fakeMapData = "{\"status\": \"OK\",\"map\":\"ID;METERS;COORDINATES;FRC;NET2CLASS;NAME;ONEWAY;KPH;SPEED;SPEEDTYP;VALDIR;VT;VERIFIED\n0001;1;0,0,0,1;1;1;0001;;1;0;;0;0;0;\n0102;4;0,1,0,2;1;1;0102;;1;0;;0;0;0;\n\"}";

    public static PassPoint parsePassPoint(String response)
    {
        PassPoint p = null;
        try
        {
            JSONObject jsonObj = new JSONObject(response);
            String status = jsonObj.getString("status");
            if (SERVER_RESPONSE_OK.equals(status))
            {
                p = new PassPoint();
                p.count = jsonObj.getInt("count");
                p.latitude = jsonObj.getDouble("latitude");
                p.longitude = jsonObj.getDouble("longitude");
                p.timestamp = jsonObj.getLong("timestamp");
            }
        } catch (JSONException e)
        {
            System.out.println("Json parse error");
            e.printStackTrace();
        }
        return p;
    }

    public static Vector<Edge> parseMapData(String response)
    {
        Vector<Edge> edges = null;
        try
        {
            JSONObject jsonObj = new JSONObject(response);
            String status = jsonObj.getString("status");
            if (SERVER_RESPONSE_OK.equals(status))
            {
                edges = new Vector<Edge>();
                String map = jsonObj.getString("map");
                if (map != null)
                {
                    String[] tokens = map.split("\n");

                    if (tokens != null && tokens.length > 1)
                    {
                        for (int i = 1; i < tokens.length; i++)
                        {
                            if (tokens[i] != null && tokens[i].length() > 0)
                            {
                                String[] p = tokens[i].split(";");
                                if (p.length == 13)
                                {
                                    int posTransTime = 0;
                                    int negTransTime = 0;
                                    int transTime = Integer.parseInt(p[1]);
                                    if (p[6].equals(""))
                                    {
                                        posTransTime = transTime;
                                        negTransTime = transTime;
                                    } else if ("FT".equalsIgnoreCase(p[6]))
                                    {
                                        posTransTime = transTime;
                                    } else if ("TF".equalsIgnoreCase(p[6]))
                                    {
                                        negTransTime = transTime;
                                    }
                                    String[] cor = p[2].split(",");
                                    if (cor.length == 4)
                                    {
                                    	int x1 = Integer.parseInt(cor[0]);
                                    	int y1 = Integer.parseInt(cor[1]);
                                    	int x2 = Integer.parseInt(cor[2]);
                                    	int y2 = Integer.parseInt(cor[3]);
                                    	if (x1 == x2 || y1 == y2) {
                                    		Point begin = PointsManager.getPoint(Math.min(y1, y2), Math.min(x1, x2));
                                    		Point end = PointsManager.getPoint(Math.max(y1, y2), Math.max(x1, x2));
                                    		edges.add(new Edge(p[0], 0, posTransTime, negTransTime, begin, end));
                                    	}
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (JSONException e)
        {
            System.out.println("Json parse error");
            e.printStackTrace();
        }
        return edges;
    }

    // @xunliu: Confirm with wangyang on return value
    public static String parseEvaluation(String response)
    {
        try
        {
            JSONObject jsonObj = new JSONObject(response);
            String status = jsonObj.getString("status");
            
            if (SERVER_RESPONSE_OK.equals(status))
            {
                return "OK";
            }
            else if (SERVER_RESPONSE_INVALID_ROUTE.equals(status))
            {
                return "INVALID_ROUTE";
            }
            else if (SERVER_RESPONSE_TIME_OUT.equals(status))
            {
                return "TIME_OUT";
            }
            else
            {
                return "";
            }
            
            
        } catch (JSONException e)
        {
            e.printStackTrace();
        }

        return null;
    }

    public static Vector<Vector<Edge>> parseHint(String response, EdgeMap edgeMap)
    {
        Vector<Vector<Edge>> result = new Vector<Vector<Edge>>();
        try
        {
            JSONObject jsonObj = new JSONObject(response);
            String status = jsonObj.getString("status");
            if (SERVER_RESPONSE_OK.equals(status))
            {
                JSONArray routes = jsonObj.getJSONArray("routes");
                JSONObject route = null;
                JSONObject edge = null;
                Vector<Edge> hint;
                Edge e = null;
                for (int i = 0; i < routes.length(); i++)
                {
                    route = routes.getJSONObject(i);
                    if (route != null)
                    {
                    	hint = new Vector<Edge>();
                        JSONArray edges = route.getJSONArray("route");
                        for (int j = 0; j < edges.length(); j++)
                        {
                            edge = edges.getJSONObject(j);
                            if (edge != null)
                            {
                                e = edgeMap.getEdgeByID(edge.getString("id"));
                                hint.add(e);
                            }
                        }
                        result.add(hint);
                    }
                }
            }
        } catch (JSONException e)
        {
            e.printStackTrace();
        }
        return result;
    }
}
